package chen.jia.xin.finalproject.MainView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import chen.jia.xin.finalproject.BaseActivity;
import chen.jia.xin.finalproject.R;

public class LitepalOperation extends BaseActivity {
    private String def_pic_date = "2021-6-13";
    private String def_pic_owner = "lyt";
    private int def_pic = R.drawable.gm;
    //多线程私有变量
    public static final int UPDATE_TEXT = 1;
    private TextView textView;
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        @SuppressLint("SetTextI18n")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                    case UPDATE_TEXT :
                        textView.setText("必须在主线程中修改UI");
                        break;
                    default:
                        break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.litepal_layout);
        Button add = findViewById(R.id.add_button);
        final EditText add_data = findViewById(R.id.add_data);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Picture picture = new Picture();
                picture.setOwner(def_pic_owner);
                picture.setPic_date(def_pic_date);
                picture.setPic_id(def_pic);
                picture.setPic_name(add_data.getText().toString());
                picture.save();
                finish();
            }
        });
        //多线程开始
        textView = findViewById(R.id.multiThread);
        Button multiThread = findViewById(R.id.start_button);
        multiThread.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将消息放入handler（异步消息机制）
                Message message = new Message();
                message.what = UPDATE_TEXT;
                handler.sendMessage(message);
            }
        });
    }
}
